package com.example.customer01.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLIntegrityConstraintViolationException;
import org.springframework.dao.DuplicateKeyException;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Map<String, Object> handleDuplicateKey(SQLIntegrityConstraintViolationException e) {
        Map<String, Object> map = new HashMap<>();
        map.put("message", "学号已存在");
        return map;
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Map<String, Object> handleDuplicateKeySpring(DuplicateKeyException e) {
        Map<String, Object> map = new HashMap<>();
        map.put("message", "学号已存在");
        return map;
    }

    @ExceptionHandler(Exception.class)
    public Map<String, Object> handleOther(Exception e) {
        Map<String, Object> map = new HashMap<>();
        map.put("message", "操作失败：" + e.getMessage());
        return map;
    }
} 